package com.xuyaxu.rabbitmq.common.utils;

import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

/**
 * rabbitmq工具类
 *
 * @author xyx
 * @date 2021/3/29 15:05
 */
public class RabbitmqUtil {

    /**
     * 从工厂中获取连接
     *
     * @return 连接
     * @throws Exception 异常
     */
    public static Connection getConnection() throws Exception {
        // 1.定义连接（rabbitmq）工厂
        ConnectionFactory factory = new ConnectionFactory();
        // 设置服务器地址
        factory.setHost("127.0.0.1");
        // 设置服务器端口
        factory.setPort(5672);
        // 设置虚拟仓库名
        factory.setVirtualHost("my_vhost");
        // 设置用户名
        factory.setUsername("guest");
        // 设置密码
        factory.setPassword("guest");
        // 2.通过工厂获取连接
        Connection connection = factory.newConnection();
        return connection;
    }
}
